package nl.hr.cmi.opleiding;

import java.util.List;

import nl.hr.cmi.opleiding.database.DBShowcaseItem;
import nl.hr.cmi.opleiding.database.DBShowcaseItemDatabaseHelper;
import nl.hr.cmi.opleiding.utils.ShowcaseView;
import nl.hr.cmi.opleiding.utils.animation.Flipper;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;
import com.j256.ormlite.dao.RuntimeExceptionDao;

public class Showcase extends OrmLiteBaseActivity<DBShowcaseItemDatabaseHelper> {

	ViewFlipper flipper;
	Button btLeft;
	Button btRight;
	TableLayout tl_featured;
	// TODO this is really bad
	private int pixel;

	private static final int LAYOUT_CASE_MARGIN = 5;

	Resources r;// TODO make this less memory and GC consuming

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.showcase);

		flipper = (ViewFlipper) findViewById(R.id.showcase_flipper);
		btLeft = (Button) findViewById(R.id.bt_left);
		btRight = (Button) findViewById(R.id.bt_right);
		tl_featured = (TableLayout) findViewById(R.id.showcase_featured_table);

		r = getResources();
		pixel = this.getWindowManager().getDefaultDisplay().getWidth();

		// DO DB things
		RuntimeExceptionDao<DBShowcaseItem, Integer> shocaseDao = getHelper().getDBShowcaseItemDao();
		// query for all of the data objects in the database
		List<DBShowcaseItem> list = shocaseDao.queryForAll();

		int x = 1; // stupid pointer

		//
		TableRow tr = new TableRow(getBaseContext());
		tr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		tr.setGravity(Gravity.CENTER);
		for (DBShowcaseItem item : list) { // TODO make this loop more pretty :)
			LinearLayout ll = new LinearLayout(getBaseContext());
			LayoutParams l = new LayoutParams((pixel / 2)-20, (pixel / 2)-20);
			l.setMargins(LAYOUT_CASE_MARGIN, LAYOUT_CASE_MARGIN, LAYOUT_CASE_MARGIN, LAYOUT_CASE_MARGIN);
			ll.setLayoutParams(l);
			ll.setBackgroundResource(R.color.wp7_blue);
			ll.setGravity(Gravity.CENTER);
			ll.setOrientation(LinearLayout.VERTICAL);

			ShowcaseView s = new ShowcaseView(tl_featured.getContext());
			s.setBackgroundResource(R.drawable.colorfinder);
			s.setLayoutParams(new android.view.ViewGroup.LayoutParams(pixel / 4, pixel / 4));
			s.setTag(item.getId()); // TODO strongtype ID

			int drawableId = r.getIdentifier(item.getAfbeelding(), "drawable", "nl.hr.cmi.opleiding");
			s.setImageResource(drawableId);// TODO make a default image when all else fails
											
			s.setBackgroundResource(drawableId);

			TextView tv = new TextView(getBaseContext());
			tv.setText(item.getTitle());
			tv.setGravity(Gravity.CENTER);
			tv.setTextColor(Color.WHITE);

			ll.addView(s);
			ll.addView(tv);
			tr.addView(ll);

			if (x % 2 != 0) {
				tl_featured.addView(tr);
			} else {
				// tl_featured.addView(tr);
				tr = new TableRow(getBaseContext());
				tr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
				tr.setGravity(Gravity.CENTER);
			}
			x++;
		}

		btRight.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				flipper.setInAnimation(Flipper.inFromRightAnimation());
				flipper.setOutAnimation(Flipper.outToLeftAnimation());
				flipper.showPrevious();
			}
		});

		btLeft.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				flipper.setInAnimation(Flipper.inFromLeftAnimation());
				flipper.setOutAnimation(Flipper.outToRightAnimation());
				flipper.showNext();
			}
		});
	}
}
